home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / NetHack 3.1.3 / source / sys / mac / macerrs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-01  |  4.2 KB  |  169 lines  |  [TEXT/R*ch]

  1. /*    SCCS Id: @(#)macerrs.c    3.1    93/01/24          */
  2. /* Copyright (c) Michael Hamel, 1991 */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. #ifdef applec    /* This needs to be resident always */
  6. #pragma segment Main
  7. #endif
  8.  
  9. #include "hack.h"
  10.  
  11. #include <OSUtils.h>
  12. #include <files.h>
  13. #include <Types.h>
  14. #ifdef MAC_MPW32
  15. #include <String.h>
  16. #include <Strings.h>
  17. #else
  18. #include <pascal.h>
  19. #endif
  20. #include <Dialogs.h>
  21. #include <Packages.h>
  22. #include <ToolUtils.h>
  23. #include <Resources.h>
  24.  
  25. #define stackDepth  4
  26. #define errAlertID 129
  27. #define stdIOErrID 1999
  28.  
  29. void FDECL(comment,(char *, long));
  30. void FDECL(showerror,(char *, const char *));
  31. Boolean FDECL(itworked,(short));
  32. void FDECL(mustwork,(short));
  33. static void VDECL(vprogerror,(const char *line, va_list the_args));
  34. void FDECL(attemptingto,( char * activity ));
  35. void FDECL(pushattemptingto,( char * activity ));
  36. void NDECL(popattempt);
  37.  
  38. static Str255 gActivities[stackDepth] = {"","","",""};
  39. static short gTopactivity = 1;
  40.  
  41. void  comment( char *s, long n )
  42. {
  43.   Str255 paserr;
  44.   short itemHit;
  45.   
  46.   sprintf((char *)paserr, "%s - %d",s,n);
  47.   ParamText(c2pstr((char *)paserr),(StringPtr)"",(StringPtr)"",(StringPtr)"");
  48.   itemHit = Alert(128, (ModalFilterProcPtr)nil);
  49. }
  50.  
  51. void showerror( char * errdesc, const char * errcomment )
  52. {
  53.        short        itemHit;
  54.     Str255        paserr,
  55.                 pascomment;
  56.                 
  57.     SetCursor(&qd.arrow);
  58.     if (errcomment == nil)  pascomment[0] = '\0';
  59.       else strcpy((char *)pascomment,(char *)errcomment);
  60.     strcpy((char *)paserr,(char *)errdesc);
  61.     ParamText(c2pstr((char *)paserr),c2pstr((char *)pascomment),gActivities[gTopactivity],(StringPtr)"");
  62.     itemHit = Alert(errAlertID, (ModalFilterProcPtr)nil);
  63. }
  64.  
  65.  
  66. Boolean itworked( short errcode )
  67. /* Return TRUE if it worked, do an error message and return false if it didn't. Error
  68.    strings for native C errors are in STR#1999, Mac errs in STR 2000-errcode, e.g
  69.    2108 for not enough memory */
  70.  
  71. {
  72.   if (errcode != 0) {
  73.     short         itemHit;
  74.     Str255          errdesc;
  75.     StringHandle strh;
  76.     
  77.     errdesc[0] = '\0';
  78.     if (errcode > 0) GetIndString(errdesc,stdIOErrID,errcode);  /* STDIO file rres, etc */
  79.     else {
  80.        strh = GetString(2000-errcode);
  81.        if (strh != (StringHandle) nil) {
  82.           memcpy(errdesc,*strh,256);
  83.           ReleaseResource((Handle)strh);
  84.        }
  85.     }
  86.     if (errdesc[0] == '\0') {  /* No description found, just give the number */
  87.        sprintf((char *)errdesc,"a %d error occurred",errcode);
  88.        (void)c2pstr((char *)errdesc);
  89.     }
  90.     SetCursor(&qd.arrow);
  91.     ParamText(errdesc,(StringPtr)"",gActivities[gTopactivity],(StringPtr)"");
  92.     itemHit = Alert(errAlertID, (ModalFilterProcPtr)nil);
  93.  
  94.   }
  95.   return(errcode==0);
  96. }
  97.  
  98. void mustwork( short errcode )
  99. /* For cases where we can't recover from the error by any means */
  100. {
  101.   if (itworked(errcode)) ;
  102.        else ExitToShell();
  103. }
  104.  
  105.  
  106. #if defined(USE_STDARG) || defined(USE_VARARGS)
  107. # ifdef USE_STDARG
  108. static void vprogerror(const char *line, va_list the_args);
  109. # else
  110. static void vprogerror();
  111. # endif
  112.  
  113. /* Macro substitute for error() */
  114. void progerror VA_DECL(const char *, line)
  115.     VA_START(line);
  116.     VA_INIT(line, char *);
  117.     vprogerror(line, VA_ARGS);
  118.     VA_END();
  119. }
  120.  
  121. # ifdef USE_STDARG
  122. static void
  123. vprogerror(const char *line, va_list the_args) {
  124. # else
  125. static void
  126. vprogerror(line, the_args) const char *line; va_list the_args; {
  127. # endif
  128.  
  129. #else  /* USE_STDARG | USE_VARARG */
  130.  
  131. void
  132. progerror VA_DECL(const char *, line)
  133. #endif
  134. /* Do NOT use VA_START and VA_END in here... see above */
  135.  
  136.     if(!index(line, '%'))
  137.         showerror("of an internal error",line);
  138.     else {
  139.         char pbuf[BUFSZ];
  140.         Vsprintf(pbuf,line,VA_ARGS);
  141.         showerror("of an internal error",pbuf);
  142.     }
  143. }
  144.  
  145. void attemptingto( char * activity )
  146. /* Say what we are trying to do for subsequent error-handling: will appear as x in an
  147.    alert in the form "Could not x because y" */
  148. {
  149.    strcpy((char *)gActivities[gTopactivity],activity);
  150.    activity = (char *)c2pstr((char *)gActivities[gTopactivity]);
  151. }
  152.  
  153. void pushattemptingto( char * activity )
  154. /* Push a new description onto stack so we can pop later to previous state */
  155. {
  156.   if (gTopactivity < stackDepth) {
  157.     gTopactivity++;
  158.     attemptingto(activity);
  159.   }
  160.   else progerror("activity stack overflow");
  161. }
  162.  
  163. void popattempt( void )
  164. /* Pop to previous state */
  165. {
  166.   if (gTopactivity > 1) --gTopactivity;
  167.                    else progerror("activity stack underflow");
  168. }
  169.